package com.promise.jokerdream.work.faction

import com.promise.jokerdream.manager.ExecutionHistoryManager
import com.promise.jokerdream.model.task.FactionTaskMapping
import com.promise.jokerdream.repository.GameApi
import com.promise.jokerdream.repository.foldWithApiException
import com.promise.jokerdream.utils.AppContext
import com.promise.jokerdream.work.BaseWork
import com.promise.jokerdream.work.WorkResult

// 帮派任务工作
class FactionTaskWork(uin: String): BaseWork(uin) {

    override val name: String
        get() = "帮派任务"

    override val priority: Int
        get() = 7
    
    private val gameApi = GameApi.getInstance()
    private val historyManager by lazy { ExecutionHistoryManager.getInstance(AppContext.get()) }
    
    override suspend fun executeWork(): WorkResult {
        return try {
            // 1. 先查询帮派任务
            val queryResult = gameApi.queryFactionTask()
            queryResult.foldWithApiException(
                onSuccess = { response ->
                    // 处理每个任务
                    response.array.forEach { task ->
                        when (task.state) {
                            0 -> {
                                // 未完成的任务，需要先完成再领取
                                completeTask(task.id)
                            }
                            1 -> {
                                // 已完成的任务，直接领取
                                claimTaskReward(task.id)
                            }
                            2 -> {
                            }
                        }
                    }
                },
                onFailure = { code, msg ->
                    historyManager.addHistory(name, msg, code)
                }
            )
            
            WorkResult.Success
        } catch (e: Exception) {
            historyManager.addHistory(name, e.message)
            WorkResult.Failure(e, e.message)
        }
    }
    
    /**
     * 完成任务
     * @param taskId 任务ID
     */
    private suspend fun completeTask(taskId: Int) {
        when (taskId) {
            11 -> {
                // 查看帮贡请求
                val result = gameApi.queryFactionContributionRequest()
                result.foldWithApiException(
                    onSuccess = { response ->
                        claimTaskReward(taskId)
                    },
                    onFailure = { code, msg ->
                        historyManager.addHistory(name, msg, code)
                    }
                )
            }
            12, 14 -> {
                // 查看帮战请求
                val result = gameApi.queryFactionWar()
                result.foldWithApiException(
                    onSuccess = { response ->
                        claimTaskReward(taskId)
                    },
                    onFailure = { code, msg ->
                        historyManager.addHistory(name, msg, code)
                    }
                )
            }
            16 -> {
                // 查看帮派要闻请求
                val result = gameApi.queryFactionStaff(1)
                result.foldWithApiException(
                    onSuccess = { response ->
                        claimTaskReward(taskId)
                    },
                    onFailure = { code, msg ->
                        historyManager.addHistory(name, msg, code)
                    }
                )
            }
            else -> {
                
            }
        }
    }
    
    /**
     * 领取任务奖励
     * @param taskId 任务ID
     */
    private suspend fun claimTaskReward(taskId: Int) {
        val result = gameApi.claimFactionTaskReward(taskId)
        result.foldWithApiException(
            onSuccess = { response ->
                historyManager.addHistory(name, response.getDescription())
            },
            onFailure = { code, msg ->
                historyManager.addHistory(name, msg, code)
            }
        )
    }
}